package com.yiwei.ydd.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.api.model.OilcardListModel;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class MyOilCardAdapter extends ItemAdapter<OilcardListModel.DatasBean, MyOilCardAdapter.DefaultHolder> {


    public MyOilCardAdapter(Context context) {
        super(context);
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_my_oil_card, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        OilcardListModel.DatasBean item = getItem(position);
        holder.item = item;
        holder.txtName.setText(item.name);
        holder.txtCardNum.setText(item.card_number);
        holder.txtPhone.setText(item.phone);
        if (item.ctype.equals("1")) {
            holder.imgIcon.setImageResource(R.mipmap.img_type_shihua);
        } else if (item.ctype.equals("2")) {
            holder.imgIcon.setImageResource(R.mipmap.img_type_shiyou);
        }
        if(item.isdefault.equals("1")){
            holder.btnDefault.setImageResource(R.mipmap.img_oil_agree_check);
        }else {
            holder.btnDefault.setImageResource(R.mipmap.img_oil_agree_uncheck);
        }
    }


    class DefaultHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.img_icon)
        ImageView imgIcon;
        @BindView(R.id.txt_name)
        TextView txtName;
        @BindView(R.id.txt_card_num)
        TextView txtCardNum;
        @BindView(R.id.txt_phone)
        TextView txtPhone;
        @BindView(R.id.view_line)
        View viewLine;
        @BindView(R.id.btn_default)
        ImageView btnDefault;
        @BindView(R.id.btn_edit)
        TextView btnEdit;
        @BindView(R.id.btn_delete)
        TextView btnDelete;
        OilcardListModel.DatasBean item;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @OnClick({R.id.btn_default, R.id.btn_edit, R.id.btn_delete})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.btn_default:
                    if (onChooseListener != null) {
                        onChooseListener.onDefault(item.id);
                    }
                    break;
                case R.id.btn_edit:
                    if (onChooseListener != null) {
                        onChooseListener.onEdit(item);
                    }
                    break;
                case R.id.btn_delete:
                    if (onChooseListener != null) {
                        onChooseListener.onDelete(item.id);
                    }
                    break;
            }
        }
    }


    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onEdit(OilcardListModel.DatasBean item);

        void onDelete(String id);

        void onDefault(String id);
    }
}
